package cn.itcast.bos.service.take_delivery.Impl;

import cn.itcast.bos.Dao.take_delicery.PromotionDao;
import cn.itcast.bos.domain.take_delivery.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.take_delivery.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class PromotionServiceImpl implements PromotionService {
    @Autowired
    private PromotionDao promotionDao;
    @Override
    public void save(Promotion promotion) {
        promotionDao.save(promotion);
    }

    @Override
    public Page<Promotion> pageQuery(Specification<Promotion> specification, Pageable pageable) {
        return promotionDao.findAll(specification,pageable);
    }

    @Override
    public PageBean<Promotion> bos_forePageQuery(int page, int rows) {
        Pageable pageable = new PageRequest(page-1,rows);
        Page<Promotion> pageData = promotionDao.findAll(pageable);
        //封装到pageBean对象中
        PageBean<Promotion> pageBean = new PageBean<>();
        pageBean.setTotalCount(pageData.getTotalElements());
        pageBean.setPageData(pageData.getContent());

        return pageBean ;
    }

    @Override
    public Promotion findOneById(Promotion model) {
        return promotionDao.findOne(model.getId());
    }

    @Override
    public Promotion findById(Integer id) {
        return promotionDao.findOne(id);
    }

    @Override
    public void updata(Date date) {
        promotionDao.updata(date);
    }
}
